#pragma once

#include "IL/il.h"
#pragma comment (lib,"DevIL.lib")

namespace NBE
{
	void loadTexFromFile(const char* fileName, TexMap* texmap, std::vector<unsigned char>& textureData)
	{
		ilInit();
		ilEnable( IL_ORIGIN_SET );
		ilOriginFunc( IL_ORIGIN_UPPER_LEFT );

		ILuint idImage = ilGenImage();
		ilBindImage( idImage );
		ilLoadImage( fileName );


		if(IL_NO_ERROR != ilGetError())
		{
			throw std::exception("Texture load error! ");
		}

		ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE);
		//convert to this type

		size_t startOffset = textureData.size();
		size_t len = ilGetInteger( IL_IMAGE_SIZE_OF_DATA ) ;
		textureData.resize(startOffset + len );
		memcpy_s(&textureData[0] + startOffset, len, ilGetData(), len);
		texmap->data = startOffset;
		texmap->dataSize = (unsigned int)len;


		texmap->w = ilGetInteger( IL_IMAGE_WIDTH );
		texmap->h = ilGetInteger( IL_IMAGE_HEIGHT);


	}
}